/**
  ******************************************************************************
  * File Name          : mbbsp.c
  * Description        : This file provides code for the configuration
  *                      of the USART instances.
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_ll_bus.h"
#include "stm32f4xx_ll_gpio.h"
#include "stm32f4xx_ll_tim.h"
#include "stm32f4xx_ll_usart.h"

#define RS485_2_TX_Pin GPIO_PIN_0
#define RS485_2_TX_GPIO_Port GPIOA
#define RS485_2_RX_Pin GPIO_PIN_1
#define RS485_2_RX_GPIO_Port GPIOA

#define RS485_2_DIR_Pin                       GPIO_PIN_8
#define RS485_2_DIR_GPIO_Port                 GPIOA
#define RS485_2_DIR(x)                        x?(RS485_2_DIR_GPIO_Port->BSRR = RS485_2_DIR_Pin):(RS485_2_DIR_GPIO_Port->BSRR = (uint32_t)RS485_2_DIR_Pin << 16U)


static __INLINE void rs485_2_io_init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};
	
  HAL_GPIO_WritePin(RS485_2_DIR_GPIO_Port, RS485_2_DIR_Pin, GPIO_PIN_RESET);
	
  GPIO_InitStruct.Pin = RS485_2_DIR_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(RS485_2_DIR_GPIO_Port, &GPIO_InitStruct);
}

/* UART4 init function */
static __INLINE void rs485_2_uart_init(void)
{
  LL_USART_InitTypeDef USART_InitStruct = {0};

  LL_GPIO_InitTypeDef GPIO_InitStruct = {0};

  LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_UART4);
  
  LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
  /**UART4 GPIO Configuration  
  PA0-WKUP   ------> UART4_TX
  PA1   ------> UART4_RX 
  */
  GPIO_InitStruct.Pin = LL_GPIO_PIN_0|LL_GPIO_PIN_1;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  GPIO_InitStruct.Alternate = LL_GPIO_AF_8;
  LL_GPIO_Init(GPIOA, &GPIO_InitStruct);

//  NVIC_SetPriority(UART4_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),RS485_UART_IRQ_PRIORITY, 0));
//  NVIC_EnableIRQ(UART4_IRQn);

  USART_InitStruct.BaudRate = 115200;
  USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
  USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
  USART_InitStruct.Parity = LL_USART_PARITY_NONE;
  USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX;
  USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
  USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
  LL_USART_Init(UART4, &USART_InitStruct);
	
  LL_USART_ConfigAsyncMode(UART4);
	
  LL_USART_Enable(UART4);
	
	RS485_2_DIR(1);
}

///* TIM11 init function */
//static __INLINE void rs485_2_timer_init(void)
//{
//  LL_TIM_InitTypeDef TIM_InitStruct = {0};

//  LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM11);

//  NVIC_SetPriority(TIM1_TRG_COM_TIM11_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),RS485_TIMER_IRQ_PRIORITY, 0));
//  NVIC_EnableIRQ(TIM1_TRG_COM_TIM11_IRQn);

//  TIM_InitStruct.Prescaler = 0;
//  TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
//  TIM_InitStruct.Autoreload = 0;
//  TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
//  LL_TIM_Init(TIM11, &TIM_InitStruct);
//  LL_TIM_DisableARRPreload(TIM11);
//  LL_TIM_SetOnePulseMode(TIM11, LL_TIM_ONEPULSEMODE_SINGLE);
//}

void RS485_2_BspInit(void)
{
	rs485_2_io_init();
	rs485_2_uart_init();
//	rs485_2_timer_init();
}

void RS485_2_UartSendBuf(const uint8_t *buf, uint32_t len)
{
	uint32_t i;
  
	RS485_2_DIR(1);
	
	for(i=0; i<len; i++)
	{
    UART4->DR = (buf[i]& (uint16_t)0x01FFU);
    while (!(UART4->SR & USART_SR_TXE));
	}
	
//	RS485_2_DIR(0);
}

//void rS4852_UartSendStr(const char *buf)
//{
//	uint32_t i;
//  
//	RS485_2_DIR(1);
//	
//	for(i=0; buf[i]!='\0'; i++)
//	{
//    UART4->DR = (buf[i]& (uint16_t)0x01FFU);
//    while (!(UART4->SR & USART_SR_TXE));
//	}
//	
//	RS485_2_DIR(0);
//}

/**
  * @brief This function handles UART4 global interrupt.
  */
//void UART4_IRQHandler(void)
//{
//	uint8_t temp;
//	if( (UART4->SR & USART_SR_RXNE) != 0x00 )
//	{
//		temp = USART3->DR;
//	}
//}

/**
  * @brief This function handles TIM1 trigger and commutation interrupts and TIM11 global interrupt.
  */
void TIM1_TRG_COM_TIM11_IRQHandler(void)
{
}

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
